/**
 * Created by jiax on 2018/12/23.
 */

import Header from '../../components/Header.vue'

export default {
	name: 'videoConference',
	components: {
		Header,
	},
	mounted: function() {
		if (this.$route.params.companyId) {
			this.belongCompany = this.$route.params.companyId;
		}
		this.findCompanyList();
		this.findSubCompany();
		this.getBindUsers();
	},
	watch: {
		belongCompany: 'getBindUsers',
	},
	methods: {
		getCompanyId: function() {
			// 公司编号
			this.companyId = localStorage.getItem("companyId");
			if (!this.companyId) {
				this.$router.push({
					path: '/home'
				});
			}
		},
		//分页size变化
		handleSizeChange: function(val) {
			this.pageInfo.pageSize = val;
			this.query(1);
		},
		//翻页
		handleCurrentChange: function(val) {
			this.query(parseInt(val));
		},
		formatNull: function(val) {
			if (val === null) {
				return '--'
			} else {
				return val
			}
		},
		transToMinute: function(spendTime) {
			if (!spendTime) {
				return 0;
			}
			return parseFloat(spendTime / 60).toFixed(2);
		},
		// 分页查询
		query: function(pageNum) {
			this.loading = true;
			var params = {
				guid: this.guid,
				companyId: this.belongCompany,
				userName: this.userName,
				zwanNumber: this.zwanNumber,
				userId: this.queryUserId
			};
			this.axios.hPost('/api/device?pageIndex=' + pageNum + '&pageSize=' + this.pageInfo.pageSize, params, (response) => {
				this.deviceData = response.list;
				console.log(response)
				this.pageInfo.totalNum = response.totalCount;
				this.pageInfo.currentPage = pageNum;
				this.loading = false;
			});
		},
		findCompanyList: function() {
			this.axios.hGet('/api/company/findAll', {}, (response) => {
				this.companyList = response;
				this.query(1);
			});
		},
		getCompanyName: function(companyId) {
			if (!companyId) {
				return '';
			}

			if (!this.companyList) {
				return '';
			}

			var companyName = "";
			while (companyId !== 0) {
				this.companyList.find(item => {
					if (item.id === companyId) {
						if (!companyName) {
							companyName = item.companyName;
						} else {
							companyName = item.companyName + " - " + companyName;
						}

						companyId = item.parentId;
						return false;
					}
				});
			}
			return companyName;
		},
		over: function($event) {
			$event.currentTarget.className = 'device-row el-row device-on';
		},

		out: function($event) {
			$event.currentTarget.className = 'device-row el-row';
		},
		// 激活设备
		activateDevice(item) {
			const loading = this.$loading({
				lock: true,
				text: this.$t('standard.activation'),
				spinner: 'el-icon-loading',
				background: 'rgba(0, 0, 0, 0.7)'
			});
			let params = {
				id: item.id,
				guid: item.guid,
				companyId: item.companyId,
				userId: item.userId
			};
			this.axios.hPost('/api/device/activation', params, (response) => {
				if (response.success) {
					this.$message.success(response.msg);
					this.query(this.pageInfo.currentPage);
				} else {
					this.$message.error(response.msg);
				}
				setTimeout(() => {
					loading.close();
				}, 500);
			});
		},
		findAllTypesDevices(item) {
			var params = {
				id: item.id,
				guid: item.guid,
				companyId: item.companyId,
				parentId: item.id,
			};
			this.axios.hPost('/api/device/getAllTypesDevice', params, (response) => {
				this.saving = false;
				if (response) {
					this.allTypesDeviceList = response;
				}
			});

		},
		getBindUsers() {
			var params = {
				companyId: this.belongCompany,
				userName: "query",
			};
			this.axios.hPost('/api/device/findUsers', params, (response) => {
				var userSelections = [];
				response.forEach(function(e) {
					if (params.companyId === null) {
						userSelections.push({
							"key": e.id,
							"value": e.realName
						});
					} else if (params.companyId === e.companyId) {
						userSelections.push({
							"key": e.id,
							"value": e.realName
						});
					}
				});
				this.userSelections = userSelections;
			});
		},
		showAllTypesDevice(item) {
			this.formTitle = item.guid + this.$t('video.deviceDetails');
			this.addFormVisible = true;
			this.detailLoading = true;
			this.findAllTypesDevices(item);
			this.detailLoading = false;
		},
		getPlantformName(plantformId) {
			if (plantformId === 0) {
				return this.$t('standard.glasses');
			} else if (plantformId === 1) {
				return this.$t('standard.android');
			} else if (plantformId === 2) {
				return this.$t('standard.pc');
			} else {
				return this.$t('standard.ios');
			}
		},
		findSubCompany() {
			this.axios.hPost('/api/device/getSubCompany', {}, (response) => {
				var subCompanyList = [];
				response.forEach(function(e) {
					subCompanyList.push({
						'id': e.id,
						"companyName": e.companyName
					})
				});
				this.subCompanyList = subCompanyList;
			});
		},
		formatPlantform(row, column, cellValue, index) {
			return this.getPlantformName(row.plantformId);
		},
		formatSpendTime(row, column, cellValue, index) {
			return this.transToMinute(row.spendTime);
		},
		formatValue(val) {
			return val ? val : this.$t('video.deviceDetails');
		}


	},
	data: function() {
		return {
			detailLoading: false,
			companyId: null,
			cid: null,
			loading: false,
			userName: null,
			zwanNumber: null,
			deviceId: null,
			guid: null,
			deviceData: [],
			//存储分页数据
			pageInfo: {
				currentPage: 1,
				pageSize: 10,
				totalNum: 0
			},
			formTitle: '',
			addFormVisible: false,
			companyList: [],
			defaultData: {},
			saving: false,
			userSelections: [],
			addForm: {
				userName: null,
			},
			belongCompany: null,
			queryUserId: null,
			allTypesDeviceList: null,
			subCompanyList: [],
			activating: null,

		}
	},
}
